package com.buss.FrontController;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.dao.ICommonDao;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.datasource.DataSourceContextHolder;
import org.jeecgframework.core.extend.datasource.DataSourceType;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.web.system.service.MutiLangServiceI;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.buss.user.entity.BnsUsersInfoEntity;
import com.buss.user.service.BnsUsersInfoServiceI;

/**
 * 前台登陆初始化控制器
 * 
 * @author hlg
 * @Date 2016-08-03
 */
@Scope("prototype")
@Controller
@RequestMapping("/frontLoginController")
public class FrontLoginController extends BaseController {
	public static final String LOCAL_FRONT_USER = "LOCAL_FRONT_USER";
	private Logger log = Logger.getLogger(FrontLoginController.class);
	
	private String message = null;
	public ICommonDao commonDao = null;

	@Autowired
	private MutiLangServiceI mutiLangService;
	
	@Autowired
	private CommonService commonService;

	@Autowired
	private SystemService systemService;
	
	@Autowired
	private BnsUsersInfoServiceI bnsUsersInfoServiceI;


	@RequestMapping(params = "goPwdInit")
	public String goPwdInit() {
		return "login/pwd_init";
	}

	
	/**
	 * 登录页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goLogin")
	public String goLogin() {
		return "front/login";
	}
	
	/**
	 * admin账户密码初始化
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "pwdInit")
	public ModelAndView pwdInit(HttpServletRequest request) {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(new RedirectView("loginController.do?login"));
		return modelAndView;
	}

	/**
	 * 检查会员名称  验证密码
	 * 
	 * @param user
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "checkuser")
	@ResponseBody
	public AjaxJson checkuser(BnsUsersInfoEntity user, HttpServletRequest req) {
		HttpSession session = ContextHolderUtils.getSession();
		DataSourceContextHolder.setDataSourceType(DataSourceType.dataSource_jeecg);
		AjaxJson j = new AjaxJson();

		String username = req.getParameter("username"); // 用户
		String password = req.getParameter("password"); // 密码

		String ask = req.getParameter("ask"); // 安全保护问题
		String ans = null;
		if (!ask.equals("0")) {
			ans = req.getParameter("ans"); // 安全问题答案
		} else {
			ans = "";
		}

		BnsUsersInfoEntity bnsUsersInfoEntity = bnsUsersInfoServiceI.checkUserExits(username, password, ask, ans); // 判断当前会员是否存在
		
		// 判断当前会员是否存在
		if (bnsUsersInfoEntity != null) {
			bnsUsersInfoEntity.setPassWord("");
			bnsUsersInfoEntity.setAsk("");
			saveLoginSuccessInfo(req, bnsUsersInfoEntity);
		} else { //用户不存在 或者密码输入错误
			j.setMsg(mutiLangService.getLang("common.username.or.password.error"));
			j.setSuccess(false);
			return j;
		}

		return j;
	}

	/**
	 * 保存用户登录的信息
	 * @param req request
	 * @param user 当前登录用户
	 * @param orgId 组织主键
	 */
	private void saveLoginSuccessInfo(HttpServletRequest req, BnsUsersInfoEntity user) {
		HttpSession session = ContextHolderUtils.getSession();
		session.setAttribute(LOCAL_FRONT_USER, user);

	}

	/**
	 * 会员登录,并把会员信息存入session
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(params = "login")
	public ModelAndView login(Model model, HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		DataSourceContextHolder.setDataSourceType(DataSourceType.dataSource_jeecg);
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute(LOCAL_FRONT_USER);
		if (userInfo != null) {
			int userId = userInfo.getUserId();
//			BnsUsersInfoEntity memberEntity = commonService.findUniqueByProperty(BnsUsersInfoEntity.class, "mbId", mbId);
//			model.addAttribute("niceName", memberEntity.getNiceName());
//			model.addAttribute("userName", memberEntity.getUserName());
//
			session.setAttribute("userName", userInfo.getUserName());
//			session.setAttribute("loginMember", userInfo.getUserName()); // 设置
//																				// 登陆会员名
			session.setAttribute("mbId", userId); // 设置 登陆会员Id

			bnsUsersInfoServiceI.addUserLog(userId+""); // 保存用户登录日志
			
			
			return new ModelAndView("redirect:/indexController.do?index"); //登录成功重定向 到会员中心

		} else {
			// return "com/web/login";
			return new ModelAndView("front/login");
		}

	}


	/**
	 * 退出系统
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "logout")
	public ModelAndView logout(HttpServletRequest request) {
		HttpSession session = ContextHolderUtils.getSession();
//		BnsUsersInfoEntity member = (BnsUsersInfoEntity) session.getAttribute(ResourceUtil.LOCAL_CLINET_USER);
		session.setAttribute("userName", "");
		session.setAttribute("mbId", "");
		ContextHolderUtils.getSession().removeAttribute(LOCAL_FRONT_USER);
		return new ModelAndView("front/login");
	}
	
	/**
	 * 找回密码
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "findPassWord")
	public ModelAndView findPassWord(HttpServletRequest request) {
//		HttpSession session = ContextHolderUtils.getSession();
////		BnsUsersInfoEntity member = (BnsUsersInfoEntity) session.getAttribute(ResourceUtil.LOCAL_CLINET_USER);
//		session.setAttribute("userName", "");
//		session.setAttribute("mbId", "");
//		ContextHolderUtils.getSession().removeAttribute(ResourceUtil.LOCAL_CLINET_USER);
		return new ModelAndView("front/findPassWord");
	}
	
	
	
}